<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MCP Demo Client</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        #chat-container {
            height: 400px;
            border: 1px solid #ccc;
            overflow-y: auto;
            margin-bottom: 20px;
            padding: 10px;
        }

        .message {
            margin: 10px 0;
            padding: 10px;
            border-radius: 5px;
        }

        .user-message {
            background-color: #e3f2fd;
            margin-left: 20%;
        }

        .bot-message {
            background-color: #f5f5f5;
            margin-right: 20%;
        }

        .tool-message {
            background-color: #fff3e0;
            margin-right: 20%;
        }

        #input-container {
            display: flex;
            gap: 10px;
        }

        #message-input {
            flex-grow: 1;
            padding: 10px;
        }

        button {
            padding: 10px 20px;
            background-color: #2196f3;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #1976d2;
        }

        #tools-container {
            margin-top: 20px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
    </style>
</head>
<body>
<h1>MCP Demo Client</h1>
<div id="chat-container"></div>
<div id="input-container">
    <input type="text" id="message-input" placeholder="Type your message...">
    <button onclick="sendMessage()">Send</button>
</div>
<div id="tools-container">
    <h3>Available Tools</h3>
    <div id="tools-list"></div>
</div>

<script>
    let ws = null;
    let tools = [];

    function connect() {
        ws = new WebSocket('ws://localhost:8000/ws');

        ws.onopen = () => {
            console.log('Connected to MCP server');
            addMessage('System', 'Connected to MCP server', 'bot-message');
        };

        ws.onmessage = (event) => {
            const data = JSON.parse(event.data);

            if (data.type === 'tools') {
                tools = data.content.tools;
                displayTools();
            } else if (data.type === 'response') {
                addMessage('Bot', data.content.message, 'bot-message');
            } else if (data.type === 'tool_response') {
                addMessage('Tool', JSON.stringify(data.content, null, 2), 'tool-message');
            }
        };

        ws.onclose = () => {
            console.log('Disconnected from MCP server');
            addMessage('System', 'Disconnected from MCP server', 'bot-message');
            // 尝试重新连接
            setTimeout(connect, 3000);
        };

        ws.onerror = (error) => {
            console.error('WebSocket error:', error);
            addMessage('System', 'Error: ' + error.message, 'bot-message');
        };
    }

    function displayTools() {
        const toolsList = document.getElementById('tools-list');
        toolsList.innerHTML = tools.map(tool => `
                <div>
                    <strong>${tool.name}</strong>: ${tool.description}
                </div>
            `).join('');
    }

    function addMessage(sender, content, className) {
        const chatContainer = document.getElementById('chat-container');
        const messageDiv = document.createElement('div');
        messageDiv.className = `message ${className}`;
        messageDiv.innerHTML = `<strong>${sender}:</strong> ${content}`;
        chatContainer.appendChild(messageDiv);
        chatContainer.scrollTop = chatContainer.scrollHeight;
    }

    function sendMessage() {
        const input = document.getElementById('message-input');
        const message = input.value.trim();

        if (message && ws && ws.readyState === WebSocket.OPEN) {
            addMessage('You', message, 'user-message');
            ws.send(JSON.stringify({
                type: 'chat',
                content: {message}
            }));
            input.value = '';
        }
    }

    // 按Enter发送消息
    document.getElementById('message-input').addEventListener('keypress', (e) => {
        if (e.key === 'Enter') {
            sendMessage();
        }
    });

    // 初始连接
    connect();
</script>
</body>
</html> 